#!/bin/bash

# 定义一个函数来执行命令并检查错误
execute_with_check() {
    "$@"  # 执行传递进来的命令
    if [ $? -ne 0 ]; then
        echo "命令执行失败: $*"
        exit 1
    fi
}

check_and_copy() {
    local source_dir="$1"
    local target_dir="$2"
    local dir_name="$3"
    # 检查目标目录是否存在
    if [ ! -d "$target_dir/$dir_name" ]; then
        execute_with_check cp -r "$source_dir" "$target_dir"
    else
        echo "目录 '$target_dir/$dir_name' 已存在，跳过复制。"
    fi
}

# 添加ROS官方GPG密钥
# execute_with_check sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
# 设置ROS Noetic的APT官方源
# execute_with_check sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

# 添加ROS清华GPG密钥
execute_with_check sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
# 设置ROS Noetic的APT清华源
execute_with_check sudo sh -c 'echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros.list'


# 更新APT源列表
execute_with_check sudo apt-get update

# 安装ROS Noetic Desktop Full
execute_with_check sudo apt install -y ros-noetic-desktop-full

#安装rosdepc并设置环境
execute_with_check sudo apt-get -y install python3-pip
execute_with_check sudo pip install rosdepc
execute_with_check sudo rosdepc init
execute_with_check rosdepc update
execute_with_check echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
execute_with_check source ~/.bashrc

# 安装所需的第三方库
execute_with_check sudo apt install -y python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
execute_with_check sudo apt install -y libvlc-dev libvlccore-dev vlc
execute_with_check sudo apt install -y ros-noetic-serial
execute_with_check sudo apt-get install -y ros-noetic-nmea-navsat-driver libgps-dev
execute_with_check sudo apt-get install -y cutecom
execute_with_check sudo apt-get install -y gdb

#安装eigen3

execute_with_check cp -r '/media/firefly/新加卷/核心板刷机/安装包/eigen-3.3.0' /home/firefly
# check_and_copy '/media/firefly/新加卷/核心板刷机/安装包/eigen-3.3.0' '/home/firefly' 'eigen-3.3.0'
execute_with_check cd /home/firefly/eigen-3.3.0
execute_with_check mkdir build
execute_with_check cd build
execute_with_check cmake ..
execute_with_check make -j8
execute_with_check sudo make install
execute_with_check sudo cp -r /usr/local/include/eigen3/Eigen /usr/local/include 

#安装g2o
execute_with_check cp -r '/media/firefly/新加卷/核心板刷机/安装包/g2o-master' /home/firefly
# check_and_copy '/media/firefly/新加卷/核心板刷机/安装包/g2o-master' /home/firefly 'g2o-master'
execute_with_check cd /home/firefly/g2o-master
execute_with_check mkdir build
execute_with_check cd build
execute_with_check cmake ..
execute_with_check make -j8
execute_with_check sudo make install
execute_with_check cd /home/firefly

#复制、设置算法文件
execute_with_check sudo cp '/media/firefly/新加卷/核心板刷机/runtime/RK3588/Linux/rknn_server/aarch64/usr/bin/restart_rknn.sh' /usr/bin
execute_with_check sudo cp '/media/firefly/新加卷/核心板刷机/runtime/RK3588/Linux/rknn_server/aarch64/usr/bin/rknn_server' /usr/bin
execute_with_check sudo cp '/media/firefly/新加卷/核心板刷机/runtime/RK3588/Linux/rknn_server/aarch64/usr/bin/start_rknn.sh' /usr/bin
execute_with_check sudo cp '/media/firefly/新加卷/核心板刷机/runtime/RK3588/Linux/librknn_api/aarch64/librknn_api.so' /usr/lib
execute_with_check sudo cp '/media/firefly/新加卷/核心板刷机/runtime/RK3588/Linux/librknn_api/aarch64/librknnrt.so' /usr/lib

execute_with_check sudo chmod +x /usr/bin/rknn_server
execute_with_check sudo chmod +x /usr/bin/start_rknn.sh
execute_with_check sudo chmod +x /usr/bin/restart_rknn.sh
execute_with_check sudo restart_rknn.sh &


#创建代码文件夹
execute_with_check mkdir /home/firefly/LC
execute_with_check cd /home/firefly/LC

execute_with_check cp -r '/media/firefly/新加卷/核心板刷机/LC/config' /home/firefly/LC
execute_with_check cp -r '/media/firefly/新加卷/核心板刷机/LC/1.1.1.4' /home/firefly/LC

#使用install文件无需编译
# execute_with_check cd /home/firefly/LC
# execute_with_check source /opt/ros/noetic/setup.sh

# execute_with_check catkin_make -DCATKIN_WHITELIST_PACKAGES="custom_msgs"
# execute_with_check catkin_make -DCATKIN_WHITELIST_PACKAGES="hipnuc_imu"
# execute_with_check catkin_make -DCATKIN_WHITELIST_PACKAGES=""

# 定义要写入的内容
CONTENT="[Unit]
Description=/etc/rc.local Compatibility 
ConditionPathExists=/etc/rc.local 
 
[Service]
Type=forking 
ExecStart=/etc/rc.local start 
TimeoutSec=0 
StandardOutput=tty 
RemainAfterExit=yes 
SysVStartPriority=99 
 
[Install]
WantedBy=multi-user.target
"
# 使用echo和重定向写入内容
execute_with_check echo "$CONTENT" | sudo tee /etc/systemd/system/rc-local.service > /dev/null

# 定义要写入的内容
CONTENT1="#! /bin/bash

source /opt/ros/noetic/setup.bash
cd /home/firefly/LC
read -r root_dir < /home/firefly/LC/config/soft_version
cd \$root_dir
source install/setup.bash
roslaunch mowing_robot mowing_robot.launch &
"
# 使用echo和重定向写入内容
execute_with_check echo "$CONTENT1" | sudo tee /etc/rc.local > /dev/null

execute_with_check sudo chmod 777 /etc/rc.local
execute_with_check sudo systemctl enable rc-local.service
execute_with_check sudo systemctl start rc-local.service


# 定义要修改的文件路径
launch_file_path="/opt/ros/noetic/share/nmea_navsat_driver/launch/nmea_serial_driver.launch"
# 使用sed命令替换port的默认值
sudo sed -i 's/<arg name="port" default="\/dev\/ttyUSB0" \/>/<arg name="port" default="\/dev\/ttyS3" \/>/g' "$launch_file_path"
# 使用sed命令替换baud的默认值
sudo sed -i 's/<arg name="baud" default="[0-9]\+" \/>/<arg name="baud" default="9600" \/>/g' "$launch_file_path"
sudo cat "$launch_file_path"
echo "nmea_serial_driver.launch 文件已成功修改。"

# 脚本结束
echo "全部安装成功，请手动配置蓝牙"
sudo cutecom
# sudo reboot